package com.soryin.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import weibo4j.Users;
import weibo4j.model.WeiboException;

import com.qq.connect.QQConnectException;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.utils.http.HttpClient;
import com.soryin.entity.OpenPlatform;
import com.soryin.entity.User;
import com.soryin.service.OpenPlatformService;
import com.soryin.service.UserService;


/**
 * 登陆请求管理
 * @author Hello_海生
 * @date 2014年3月21日
 */
@SuppressWarnings("unused")
@Controller		//注解该类为 controller
@RequestMapping("/login")		//用于处理 /login 
public class LoginController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private OpenPlatformService openPlatformService;
	
	@RequestMapping("/SinaWB")	//新浪微博登陆
	public void sinaWB(String uid,String token,String auto,HttpServletRequest request,HttpServletResponse response) throws IOException, WeiboException{
		PrintWriter out = response.getWriter();	//方面输出JSON内容

		System.out.println("用户新浪Token："+token+"\n用户新浪Uid："+uid);
		//查询该用户是否登陆过 如果未登陆过,则把信息保存到数据库
		OpenPlatform op=openPlatformService.selectOpenPlatformByTypeAndSid("SinaWB",token);
		if(op.getId()==0 && "true".equals(auto)){
			//获取用户微博信息
			Users um = new Users();
			um.client.setToken(token);
			weibo4j.model.User wb = um.showUserById(uid);		//获取用户信息
			
			User user =new User();
			user.setName(wb.getScreenName());		// 设置人物昵称等于  微博昵称
			user.setPhoto(wb.getavatarLarge());		//设置 用户头像
			op.setUserId(user);
			op.setSid(token);
			op.setType("SinaWB");
			
			this.autoReg(op,response);	//自动注册
			
		}else if(op.getId()!=0){
			//根据ID 查询用户信息 并存到Session
			User u = userService.findById(op.getUserId().getId());	
			this.commLogin(u, response);
		}else{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("Message", "登陆失败！原因是：你账号未注册。");
			map.put("Static", "ERROR");
			String reJson=JSON.toJSONString(map);
			out.write(reJson);
		}
		
	}
	
	@RequestMapping("/TencentQQ")	//腾讯QQ登陆
	public void tencentQQ(String uid,String token,String auto,HttpServletRequest request,HttpServletResponse response) throws IOException, QQConnectException{
		PrintWriter out = response.getWriter();	//方面输出JSON内容
		System.out.println("用户腾讯Token："+token+"\n用户腾讯Uid："+uid);
		//查询该用户是否登陆过 如果未登陆过,则把信息保存到数据库
		OpenPlatform op=openPlatformService.selectOpenPlatformByTypeAndSid("TencentQQ",token);
		if(op.getId()==0 && "true".equals(auto)){
			//获取用户信息
            UserInfo qzoneUserInfo = new UserInfo(token, uid);	//获取用户信息
            UserInfoBean userInfo = qzoneUserInfo.getUserInfo();
            
			User user =new User();
			user.setName(userInfo.getNickname());		// 设置人物昵称等于  微博昵称
			user.setPhoto(userInfo.getAvatar().getAvatarURL100());		//设置 用户头像
			op.setUserId(user);
			op.setSid(token);
			op.setType("TencentQQ");
			
			this.autoReg(op,response);	//进行自动注册
		}else if(op.getId()!=0){
			//根据ID 查询用户信息 并存到Session
			User u = userService.findById(op.getUserId().getId());	
			this.commLogin(u, response);
		}else{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("Message", "登陆失败！原因是：你账号未注册。");
			map.put("Static", "ERROR");
			String reJson=JSON.toJSONString(map);
			out.write(reJson);
		}
		
	}
	
	@RequestMapping("/Normal")	//正常登陆
	public void normal(User user,HttpServletRequest request,HttpServletResponse response) throws IOException{
		PrintWriter out = response.getWriter();	//方面输出JSON内容
		System.out.println("用户Id："+user.getId()+"\n用户密码："+user.getPassword());
		User u =userService.findById(user.getId());
		if(u==null){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("Message", "登陆失败！原因是：你账号未注册。");
			map.put("Static", "ERROR");
			String reJson=JSON.toJSONString(map);
			out.write(reJson);
		}else{
			this.commLogin(u, response);
		}
	}
	
	
	
	
	
	
	
	
	
	/**
	 * 自动注册
	 * @param op
	 * @throws IOException 
	 */
	public void autoReg(OpenPlatform op,HttpServletResponse response) throws IOException{
		PrintWriter out = response.getWriter();	//方面输出JSON内容
		if((Integer)openPlatformService.save(op)==0){
			Map<String, Object> msgMap = new HashMap<String, Object>();
			msgMap.put("Static", "ERROR");
			msgMap.put("Message", "用户自动注册失败！");
			String msg=JSON.toJSONString(msgMap);
			out.write(msg);	//返回登陆失败信息
		}else{
			Map<String, Object> msgMap = new HashMap<String, Object>();
			msgMap.put("Static", "SUCCESS");
			msgMap.put("Message", "用户自动注册成功！");
			String msg=JSON.toJSONString(msgMap);
			out.write(msg);	//返回登陆失败信息
		}
	}
	
	
	/**
	 * 公共登陆 处理
	 * @param op
	 * @param response
	 * @throws IOException
	 */
	public void commLogin(User u,HttpServletResponse response) throws IOException{
		PrintWriter out = response.getWriter();	//方面输出JSON内容
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("User", u);
		map.put("Static", "SUCCESS");
		map.put("Message", "登陆成功！");
		String reJson=JSON.toJSONString(map);
		out.write(reJson);
	}
	
	
	
}
